package ut.servlet.member;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
public class CreateTask extends HttpServlet{
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		// 確認使用者已經完成信箱驗證
		ut.data.User user = (ut.data.User)request.getSession().getAttribute("user");
		if(user.verify == 0){
			return;
		}
		// 取得並驗證使用者輸入資訊
		String domain, description, content;
		int from, to, price, days, wordNumber, characterNumber;
		long publishTime, deadTime;
		if(request.getParameter("domain") == null || request.getParameter("description") == null || request.getParameter("content") == null || request.getParameter("from_lan") == null || request.getParameter("to_lan") == null || request.getParameter("price") == null || request.getParameter("publish_time") == null || request.getParameter("dead_time") == null){
			return;
		}else{
			domain = request.getParameter("domain");
			description = request.getParameter("description");
			content = request.getParameter("content");
			characterNumber = content.length();
			if(domain.length() > 30 || description.length() > 300 || content.length() > 50000){
				return;
			}
			try{
				from = Integer.parseInt(request.getParameter("from_lan"));
				to = Integer.parseInt(request.getParameter("to_lan"));
				if(ut.res.Language.getLanguage(from) == null || ut.res.Language.getLanguage(to) == null || from == to){
					return;
				}
				price = Integer.parseInt(request.getParameter("price"));
				if(price < 100 || price > 25000 || price > user.money){ // 驗證價格範圍，一百以上，不得超過目前的資金
					return;
				}
				publishTime = Long.parseLong(request.getParameter("publish_time"));
				deadTime = Long.parseLong(request.getParameter("dead_time"));
				long differDay = (deadTime - publishTime) / 86400000; // 86400000 是一天的毫秒數
				if(differDay < 1 || differDay > 180){
					return;
				}
			}catch(NumberFormatException e){
				return;
			}
			// 計算字數
			if(from == 1 || from == 3 || from == 4){ // 中、日、韓
				wordNumber = content.replaceAll("\\s+", "").length();
			}else{
				String trimContent = content.replaceAll("\\s+", " "); // 先將連續的空白字元換成一個空白
				int trimContentLength = trimContent.replaceAll("\\s+", "").length(); // 將空白拿掉後的長度
				if(trimContentLength == 0){ // 相減得到拼音字字數
					wordNumber = 0;
				}else{
					wordNumber = trimContent.length() - trimContentLength + 1;
				}
			}
			// 處理跳脫字元
			domain = domain.replace("&", "&amp;").replace("\"", "&quot;").replace("<", "&lt;").replace(">", "&gt;");
			description = description.replace("&", "&amp;").replace("\"", "&quot;").replace("<", "&lt;").replace(">", "&gt;");
			content = content.replace("&", "&amp;").replace("\"", "&quot;").replace("<", "&lt;").replace(">", "&gt;");
			// 處理換行字元
			description = description.replace("\r\n", "<br/>").replace("\n", "<br/>").replace("\r", "<br/>");
			content = content.replace("\r\n", "<br/>").replace("\n", "<br/>").replace("\r", "<br/>");
		}
		// 將新的付費案件資料加入資料庫
		response.setHeader("result", ut.data.Task.createTask(user, from, to, domain, description, content, wordNumber, characterNumber, price, publishTime / 1000, deadTime / 1000) + ""); // true: 成功、false: 失敗
	}
}